import xlrd

class TT_Excel(object):

    def ExcelRead(self,namepath,sheetname:str):
        readbook = xlrd.open_workbook(namepath)
        self.book = readbook.sheet_by_name(sheetname)
        self.mergedata = self.book.merged_cells

    def GetMergeData(self,row,col):
        for (rlow, rhigh, clow, chigh) in self.mergedata:  # 遍历表格中所有合并单元格位置信息
            print(rlow,rhigh,clow,chigh)
            if (rlow <= row < rhigh and clow <= col < chigh):  # 行坐标判断
                # 如果满足条件，就把合并单元格第一个位置的值赋给其它合并单元格
                return str(self.book.cell(rlow,clow).value)
        return ""
    
    def GetData(self,row, col:int):
        celltmp = self.book.cell(row,col)
        if(celltmp.value != None and celltmp.value != ""):
            return str(celltmp.value)
        return self.getmergedata(row,col)

    def GetDatac(self, row, strcol:str):
        strcol = strcol.upper()
        return self.getdata(row, ord(strcol[0]) - ord('A'))

    def GetRows(self):
        return self.book.nrows
    
    def GetCols(self):
        return self.book.ncols